package com.prik.observer.order.subject;

import com.prik.observer.order.observer.Observer;
import com.prik.observer.order.pojo.Order;

import java.util.ArrayList;

/**
 * 订单支付 主题
 *
 * @author: prik
 * @create: 2019-07-25
 **/
public class OrderPaidSubject implements Subject<Order> {

    /**
     * 观察者名单
     */
    private ArrayList<Observer> observerList;

    public OrderPaidSubject() {
        observerList = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer o) {
        observerList.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observerList.remove(o);
    }

    /**
     * 遍历观察者名单发送通知
     * @param order
     */
    @Override
    public void notifyObservers(Order order) {
        observerList.forEach(observer -> observer.update(order));
    }
}